package packml;

import java.rmi.RemoteException;

import javax.swing.Timer;



public class CheckSuspendState extends Packmlhandler implements Runnable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	 private volatile static Timer suspendTimer;
	 public  volatile Clock2 stopClock2 = new Clock2();
	 private int count;
	 private long time,checkTime;
	 public CheckSuspendState(){
	// ActionListener timeListener = new SuspendTimerListener();
	// suspendTimer = new Timer(1000,timeListener) ;
}
	
	@Override
	public void run() {
		while(true)
		{
			delay(1000);
			checkSuspendState();
		}
		
	}

	
	public synchronized void delay(int i) {
		try {
			wait(i);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

//	             
	           
	 
	
	public void checkSuspendState(){
//
		
		try {
			time = packmlhandlerWorkCellController.getTimeSinceLastBrick();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long now = System.currentTimeMillis();
		if(now-time>=12000){
			brick = "UnAvailable";	
//			System.out.println("Check 1 "+brick);
		}
		else{
			brick = "Available";	
//	System.out.println("Check "+brick);
		}
		
		
		
	}
		//		if(now-time>=12000 && state=="Execute"){
//			
//			brick="UnAvailable";
//			System.out.println("Check "+brick);
//			}
//			else{
//				if(now-time>=12000)
//				
//		 }
	}
	
	


//class SuspendTimerListener implements ActionListener{
//	
//    public void actionPerformed(ActionEvent e){
//    	
//			
//			
//        
//       // here only if state is execute and there are no detected bricks
//       
//        	while(brick!="Available" && stopClock2.Seconds<=11 && state=="Execute"){
//        		stopClock2.increaseSeconds();
//        	}
////        	stopClock2.resetClock();
//        	suspendTimer.stop();
//        	if(stopClock2.Seconds>=11)
//        	receiveData1 = 3;
//        }
//        
//       
//        }

//if(brick=="Available" && state=="Execute"){
//	
//		 //if(brick=="Available"){
//	        	stopClock2.resetClock();
//	        	suspendTimer.stop();
//		        
//1		        brick=null;
//	        //}
//		
//		
//
//	
//	}
//	
//	 else if(brick!="Available" && state=="Execute"){
//		 suspendTimer.start(); 
//	 }
